package jangdolbaeng2.admin.dao;

import java.util.HashMap;
import java.util.List;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.CategoryVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.SellerVO;
import jangdolbaeng2.vo.ShopVO;
import jangdolbaeng2.vo.ZipcodeVO;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class AdminDao {
	SqlSessionFactory factory = MybatisConfig.getSqlSessionFactory();
	SqlSession sqlSession;
	
	public void join(SellerVO sellerVO)
	{
		try
		{
			sqlSession = factory.openSession(); 
			sqlSession.insert("admin.join", sellerVO); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
	}
	
	public void shopjoin(ShopVO shopVO)
	{
		try
		{
			sqlSession = factory.openSession(); 
			sqlSession.insert("admin.shopjoin", shopVO); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
	}
	
	public List<ZipcodeVO> listZipcode(String searchText)
	{
		List<ZipcodeVO> listZipcode = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			listZipcode = sqlSession.selectList("admin.listZipcode", searchText); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return listZipcode;
	}
	
	public List<ProductVO> listProduct(int shop_seq)
	{
		List<ProductVO> listProduct = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			listProduct = sqlSession.selectList("admin.listProduct", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		
		return listProduct;
	}
	
	public String shopName(int shop_seq)
	{
		String name = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			name = sqlSession.selectOne("admin.shopName", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return name;
	}
	
	public ProductVO selectProduct(int product_seq)
	{
		ProductVO vo = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			vo = sqlSession.selectOne("admin.selectProduct", product_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return vo;
	}
	
	public ProductVO selectProductDetail(int product_seq)
	{
		ProductVO vo = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			vo = sqlSession.selectOne("admin.selectProduct", product_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return vo;
	}
	
	
	public void updateProduct(ProductVO vo)
	{
		int result = 0;
		
		try
		{
			sqlSession = factory.openSession(); 
			result = sqlSession.update("admin.updateProduct", vo); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.printf("%d개의 레코드를 수정하였습니다\n", result);
	}
	
	public void insertProduct(ProductVO vo)
	{
		int result = 0;
		
		try
		{
			sqlSession = factory.openSession(); 
			result = sqlSession.insert("admin.insertProduct", vo); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.printf("%d개의 레코드를 삽입하였습니다\n", result);
	}
	
	public void deleteProduct(int product_seq)
	{
		int result = 0;
		
		try
		{
			sqlSession = factory.openSession(); 
			result = sqlSession.delete("admin.deleteProduct", product_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.printf("%d개의 레코드를 삭제하였습니다\n", result);
	}
	
	public List<HashMap<String, Object>> totalDay(int shop_seq)
	{
		List<HashMap<String, Object>> totalDayList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			totalDayList = sqlSession.selectList("admin.totalDay", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.totalDay()> " + totalDayList);
		return totalDayList;
	}
	
	public List<HashMap<String, Object>> shopDay(int shop_seq)
	{
		List<HashMap<String, Object>> shopDayList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			shopDayList = sqlSession.selectList("admin.shopDay", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.shopDay()> " + shopDayList);
		return shopDayList;
	}
	
	public List<HashMap<String, Object>> totalWeek(int shop_seq)
	{
		List<HashMap<String, Object>> totalWeekList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			totalWeekList = sqlSession.selectList("admin.totalWeek", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.totalWeek()> " + totalWeekList);
		return totalWeekList;
	}
	
	
	public List<HashMap<String, Object>> shopWeek(int shop_seq)
	{
		List<HashMap<String, Object>> shopWeekList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			shopWeekList = sqlSession.selectList("admin.shopWeek", shop_seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.shopWeek()> " + shopWeekList);
		return shopWeekList;
	}
	
	
	public List<HashMap<String, Object>> ctotalDay(String categorytxt)
	{
		List<HashMap<String, Object>> c_totalDayList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			c_totalDayList = sqlSession.selectList("admin.ctotalDay", categorytxt); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return c_totalDayList;
	}
	
	public List<HashMap<String, Object>> categoryDay(String categorytxt)
	{
		List<HashMap<String, Object>> categoryDayList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			categoryDayList = sqlSession.selectList("admin.categoryDay", categorytxt); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.categoryDay()> " + categoryDayList);
		return categoryDayList;
	}
	
	public List<HashMap<String, Object>> ctotalWeek(String categorytxt)
	{
		List<HashMap<String, Object>> c_totalWeekList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			c_totalWeekList = sqlSession.selectList("admin.ctotalWeek", categorytxt); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.ctotalWeek()> " + c_totalWeekList);
		return c_totalWeekList;
	}
	
	public List<HashMap<String, Object>> categoryWeek(String categorytxt)
	{
		List<HashMap<String, Object>> categoryWeekList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			categoryWeekList = sqlSession.selectList("admin.categoryWeek", categorytxt); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.println("AdminDao.categoryWeek()> " + categoryWeekList);
		return categoryWeekList;
	}
	
	
	public List<HashMap<String, Object>> totalVolumeList()
	{
		List<HashMap<String, Object>> totalVolumeList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			totalVolumeList = sqlSession.selectList("admin.totalVolumeList"); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return totalVolumeList;
	}
	
	
	public List<HashMap<String, Object>> shopMonth(int seq)
	{
		List<HashMap<String, Object>> shopMonthList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			shopMonthList = sqlSession.selectList("admin.shopMonthList", seq); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return shopMonthList;
	}
	
	
	
	
	
	
	public List<HashMap<String, Object>> ctotalVolumeList()
	{
		List<HashMap<String, Object>> ctotalVolumeList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			ctotalVolumeList = sqlSession.selectList("admin.ctotalVolumeList"); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return ctotalVolumeList;
	}
	
	
	public List<ShopVO> shopNameList()
	{
		List<ShopVO> shopNameList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			shopNameList = sqlSession.selectList("admin.shopNameList"); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		//System.out.println("AdminDao.shopNameList: " + shopNameList);
		
		return shopNameList;
	}
	
	public List<CategoryVO> categoryNameList()
	{
		List<CategoryVO> categoryNameList = null;
		
		try
		{
			sqlSession = factory.openSession(); 
			categoryNameList = sqlSession.selectList("admin.categoryNameList"); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		return categoryNameList;
	}
	
	public void sellerDelete(String seller_id)
	{
		int result = 0;
		
		try
		{
			sqlSession = factory.openSession(); 
			result = sqlSession.delete("admin.sellerDelete", seller_id); 
			sqlSession.commit(); 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(sqlSession != null)
				sqlSession.close(); 
		}
		
		System.out.printf("%d개의 레코드를 삭제하였습니다\n", result);
	}
	
	
	
	
	
	
	/*
	 * 작성자: 박성곤
	 * 기능: 로그인
	 */
	
	public SellerVO login(String seller_id) {
		
		SellerVO seller = null;
		
		try	{
			sqlSession = factory.openSession(); 
			seller = sqlSession.selectOne("admin.login", seller_id);			 
		}
		catch(Exception e){	
			e.printStackTrace();
		}
		finally	{
			if(sqlSession != null)	sqlSession.close(); 
		}
	
		System.out.println("AdminDao.login()> seller: " + seller);
		
		return seller;
	}//login()




	public List<ShopVO> shopList(int shop_seq) {
		
		List<ShopVO> shopList = null;
		
		try	{
			sqlSession = factory.openSession(); 
			shopList = sqlSession.selectList("admin.shopList", shop_seq);			
		}
		catch(Exception e){	
			e.printStackTrace();
		}
		finally	{
			if(sqlSession != null)	sqlSession.close(); 
		}
		
		return shopList;
	}//shopList()
	
	public List<SellerVO> sellerList(HashMap<String, String> map) {
		
		List<SellerVO> sellerList = null;
		
		try	{
			sqlSession = factory.openSession(); 
			sellerList = sqlSession.selectList("admin.sellerList", map);			
		}
		catch(Exception e){	
			e.printStackTrace();
		}
		finally	{
			if(sqlSession != null)	sqlSession.close(); 
		}
	
		return sellerList;
	}


	public int getShop_seq(String seller_id) {
	
	int getShop_seq = 0;
	
	try	{
		sqlSession = factory.openSession(); 
		getShop_seq = sqlSession.selectOne("admin.getShop_seq", seller_id);			 
	}
	catch(Exception e){	
		e.printStackTrace();
	}
	finally	{
		if(sqlSession != null)	sqlSession.close(); 
	}
	
	System.out.println("시퀀스!!!!! " + getShop_seq);
	
	return getShop_seq;
	
}

	public void blogjoin(int shop_seq) {
		int result = 0;		
		try	{
			sqlSession = factory.openSession(); 
			result = sqlSession.insert("admin.blogjoin", shop_seq);	
			sqlSession.commit();
		}
		catch(Exception e){	
			e.printStackTrace();
		}
		finally	{
			if(sqlSession != null)	sqlSession.close(); 
		}		
		System.out.println("AdminDao.blogjoin()> result: " + result);
	}

	public int getSeq(String seller_id) {
		int seq = 0;		
		try	{
			sqlSession = factory.openSession(); 
			seq = sqlSession.selectOne("admin.getSeq", seller_id);			
		}
		catch(Exception e){	
			e.printStackTrace();
		}
		finally	{
			if(sqlSession != null)	sqlSession.close(); 
		}		
		System.out.println("AdminDao.getSeq()> seq: " + seq);
		
		return seq;		
	}

}//class
